// pages/diary-details/diary-details.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // diaryId:"",//日记id
    diaryId: 1, //日记id
    pageSize: 10,
    pageNum: 1,
    // content: '', //内容
    // replyId: '', // 评论指定id
    arraydata: "", //下面赋的数据
    userdata: "", //下面赋的数据
    bookid: "", //日记本id
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    pinglun: "", //评论数据
    time: '', //转的时间错
    kong: '', //发布后清空
    isshow: 0, //默认点赞
    options: '', //默认
    usid: wx.getStorageSync('userid'),

    isreply: false, // 是否为回复 false.直接发布 true.回复
    replyContent: '', // 回复内容
    replyPeople: '', // 回复的人(拼接后的) 格式:回复 ***:
    replyid: '', // 评论指定id
    replyname: '', // 回复的人的昵称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    console.log("q1q11w1w", options)
    that.setData({
      bookid: options.bookid,
      diaryId: options.diaryId,
      options: options,
    })
    //美丽日记列表上边内容
    app.request({
      url: 'doctor/diaryBook/detail', //仅为示例，并非真实的接口地址
      data: {
        bookId: that.data.bookid, //日记本id
      },
      success(res) {
        that.setData({
          userdata: res.data,
          time: app.timeData(res.data.diaryBook.addTime),
        })
      }
    })
    this.details(); //日记详情
    this.obtain(); //获取日记评论
    this.browse(); //+浏览量
  },
  //美丽日记详情 
  details: function() {
    var that = this;
    //美丽日记详情
    app.request({
      url: 'doctor/diary/detail', //仅为示例，并非真实的接口地址
      data: {
        diaryId: that.data.diaryId
      },
      success(res) {
        that.setData({
          arraydata: res.data
        })
      }
    })
  },
  //获取日记评论
  obtain: function() {
    //获取日记评论
    var that = this;
    app.request({
      url: 'doctor/diary/commentPage', //仅为示例，并非真实的接口地址
      data: {
        diaryId: that.data.diaryId, //日记id
        pageSize: that.data.pageSize, //每页条数
        pageNum: that.data.pageNum, //页数
      },
      success(res) {
        that.setData({
          pinglun: res.data
        })
      }
    })
  },
  //浏览量增加
  browse: function() {
    var that = this;
    app.request({
      url: 'doctor/diary/addDiaryViews',
      data: {
        diaryId: that.data.diaryId,
      },
      success(res) {
      }
    })
  },


  // 处理点赞按钮
  handlePoint: function(e) {
    var that = this;
    if (e.currentTarget.dataset.isshow == 0 && e.currentTarget.dataset.id == that.data.diaryId) {
      app.request({
        url: 'doctor/diary/praise',
        data: {
          diaryId: that.data.diaryId,
        },
        success(res) {
          that.details();
          that.setData({
            id: that.data.diaryId,
          });
        }
      })
    }
  },
  // 内容
  save: function(e) {
    this.setData({
      replyContent: e.detail.value,
    })
  },

  // 回复指定人
  replyPeople: function(e) {
    console.log('回复指定人:', e);
    var commentid = e.currentTarget.dataset.commentid; // 回复的评论id
    var nickname = e.currentTarget.dataset.nickname; // 回复人的昵称
    var replyPeople = '回复 ' + nickname + ':';
    this.setData({
      replyid: commentid,
      replyname: nickname,
      replyPeople: replyPeople,
    })
  },

  // 处理回复
  bindReply: function(e) {
    var exampleId = this.data.diaryId; // 日记本id
    var replyContent = this.data.replyContent; // 回复内容
    var replyPeople = this.data.replyPeople; // 回复的人 拼接后的
    if (!replyContent) {
      wx.showToast({
        title: '请输入内容',
        icon: 'none',
      })
      return false;
    }
    var isReply = replyContent.indexOf(replyPeople); // 是否为回复人或者为直接发布
    console.log('是否在:', replyContent.indexOf(replyPeople));
    var replyId = ''; // 评论指定id
    if (isReply == -1) {
      console.log('普通回复2');
    } else {
      console.log('回复指定id1', this.data.replyid);
      replyId = this.data.replyid;
      replyContent = replyContent.replace(replyPeople, '');
    }
    var that = this;
    app.request({
      url: 'doctor/diary/comment',
      data: {
        diaryId: exampleId,
        content: replyContent,
        replyId: replyId,
      },
      success(res) {
        that.setData({
          'pageNum': 1,
        })
        that.obtain();
        that.details();
        that.setData({
          'replyPeople': '',
          'replyid': '',
          'replyContent': '',
        })
      }
    })
  },
  // 使页面滚动到底部
  pageScrollToBottom: function() {
    wx.createSelectorQuery().select('#j_page').boundingClientRect(function(rect) {
      console.log('底部：', rect);
      // 使页面滚动到底部
      wx.pageScrollTo({
        scrollTop: rect.height
      })
    }).exec();
  },

  // 关注按钮
  bindFollow: function(e) {
    var userid = e.currentTarget.dataset.userid;
    var isfollow = e.currentTarget.dataset.isfollow;
    var that = this;
    if (isfollow == 1) {
      app.request({
        url: 'doctor/member/attention',
        data: {
          userId: userid,
        },
        success(res) {
          wx.showToast({
            title: '取消关注',
            icon: 'success',
            duration: 1000
          })
          that.onLoad(that.data.options);
        }
      })
    } else {
      app.request({
        url: 'doctor/member/attention',
        data: {
          userId: userid,
        },
        success(res) {
          wx.showToast({
            title: '关注成功',
            icon: 'success',
            duration: 1000
          })
          that.onLoad(that.data.options);
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // 关注
  onShareAppMessage: function() {
    return {
      title: '分享好友获取美丽币！',
      path: '/pages/index/index',
      success: function() {
        app.request({
          url: 'doctor/memberTaskApi/addBeauty',
          data: {
            userId: wx.getStorageSync('userid'),
            type: 3
          },
          success(res) {
            console.log('分享给美丽币给美丽值', res);
          }
        })
      }
    }
  }
})